import { notification } from 'antd';
// import { useModel } from 'umi'
import { ApolloClient, InMemoryCache, ApolloProvider, useQuery, gql } from "@apollo/client";
import { requestURLConfig } from './_requestURL';
import { history } from 'umi';
const isDev = process.env.NODE_ENV === 'development';
// const masterProps = useModel('@@qiankunStateFromMaster');
// console.log(masterProps)
const microAppName = (<any>window).microAppName;
console.log(microAppName)

export function requestURLPreFix() {
    if (isDev) {
        return requestURLConfig.httpDEVAPIURL
    }
    return requestURLConfig.httpAPIURL
}

const requestCode: any = {
    "200": ['success', '请求成功'],
    "301": ['error', '请求失败'],
}
const openNotificationWithIcon = (code: string, msg: string) => {
    notification[requestCode[code][0]]({
        message: requestCode[code][1],
        description: msg ? msg : "",
    });
};
export function conventionResponseDeal(response: Response) {
    if (response.code !== 200) {
        console.log(response.code)
        openNotificationWithIcon(response.code.toString(), response.msg)
    }
    if (isDev) {
        return response.data
    }
    return requestURLConfig.httpAPIURL
}

const client = new ApolloClient({
    uri: `${isDev ? requestURLConfig.devgqlAPIURL : requestURLConfig.gqlAPIURL}/${microAppName}/graphql`,
    cache: new InMemoryCache()
});

export async function gqlQuery(gqlword: string) {
    console.log(`${isDev ? requestURLConfig.devgqlAPIURL : requestURLConfig.gqlAPIURL}/${microAppName}/graphql`)
    return await client.query({query: gql`query get_${gqlword} {${gqlword}}`});
}
